<?php
class knowledge extends Model {
	/**
	 * 获取知识点
	 *
	 * @param 科目id $sub_id
	 *        	如果是0则此参数无效
	 * @param 父知识点id $p_id
	 *        	默认为0（0为跟节点）
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getknowsFromSubId($sub_id, $p_id = 0) {
		if ($sub_id == 0) {
			$sql = "SELECT * FROM  `knowledge` WHERE  `parent_id` =" . $p_id;
		} else {
			$sql = "SELECT * FROM  `knowledge` WHERE  `parent_id` =" . $p_id . " AND  `sub_id` =" . $sub_id;
		}
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 删除知识点
	 *
	 * @param unknown_type $knows_id        	
	 * @return -1还有子类不能删除 , 0 :失败, 1: 成功
	 */
	public function delknows($knows_id) {
		if ($this->getknowsFromSubId ( 0, $knows_id )) {
			return - 1;
		} else {
			$sql = "DELETE FROM `howdo`.`knowledge` WHERE `knowledge`.`knows_id` = " . $knows_id;
			return $this->del ( $sql ) ? 1 : 0;
		}
	}
	
	/**
	 * 根据知识点ID获取知识点详细信息
	 *
	 * @param unknown_type $knowid        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getKnowinfoByID($knowid) {
		$sql = "SELECT * FROM `knowledge` WHERE `knows_id` = '" . $knowid . "'";
		return $this->fetchRow ( $sql );
	}
	
	/**
	 * 检索知识点名称
	 *
	 * @param unknown_type $know        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function searchknows($know, $subid) {
		$sql = "SELECT `knowledge`.* 
				FROM `knowledge` 
				WHERE `knows_name` LIKE '%" . $know . "%'
				AND `sub_id` = '" . $subid . "'";
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 添加知识点
	 *
	 * @param unknown_type $name        	
	 * @param unknown_type $pid        	
	 * @param unknown_type $subid        	
	 * @param unknown_type $userid        	
	 * @param unknown_type $cont        	
	 * @return Ambigous <boolean, number>
	 */
	public function addknows($name, $pid, $subid, $userid, $cont) {
		$sql = "INSERT INTO `knowledge` (`knows_name`,`parent_id`,`sub_id`,`knows_create_admin_id`,`knows_cont`,`knows_create_time`)
				VALUES ('" . $name . "','" . $pid . "','" . $subid . "','" . $userid . "','" . $cont . "',NOW())";
		//echo $sql;
		return $this->insert ( $sql );
	}
	
	/**
	 * 获取知识点
	 *
	 * @param unknown_type $knowsid        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getknowsfromid($knowsid) {
		$sql = "SELECT * FROM `knowledge` WHERE `knows_id` = '" . $knowsid . "' LIMIT 1";
		return $this->fetchRow ( $sql );
	}
	
	/**
	 * 修改知识点
	 *
	 * @param unknown_type $id        	
	 * @param unknown_type $name        	
	 * @param unknown_type $cont        	
	 * @return resource
	 */
	public function updateknows($id, $name, $cont) {
		$sql = "UPDATE `knowledge`
				SET `knows_name` = '" . $name . "',
					`knows_cont` = '" . $cont . "'
				WHERE `knows_id` = '" . $id . "'	
				";
		return $this->update ( $sql );
	}
	
	/**
	 * 获取知识点列表
	 *
	 * @author linkai
	 * @version 2013-11-27 下午5:11:39
	 * @return $list
	 */
	public function getKnowledgeList($subIDList,$fatherID) {
		if ($subIDList == - 1) {
			$subID = "";
		} else {
			$subIDs = split ( "_", $subIDList );
			$subID = "WHERE `parent_id`=$fatherID AND `sub_id`=$subIDs[0] ";
			for($i = 1; $i < count ( $subIDs ); $i ++) {
				$subID .= "OR `sub_id`=$subIDs[$i] ";
			}
		}
		$sql = "SELECT `knows_id`,`knows_name` 
				FROM `knowledge` ";
		$sql .= $subID . "ORDER BY `knows_id` ASC ";
		return $this->fetchAll ( $sql );
	}
	
	/**
	 * 查重
	 *
	 * @param unknown_type $knowName        	
	 * @param unknown_type $subId        	
	 * @param unknown_type $knowId        	
	 * @return Ambigous <boolean, multitype:>
	 */
	public function check($knowName, $subId, $knowId = 0) {
		if ($knowId) {
			$sql = "SELECT * FROM `knowledge` WHERE `knows_id` != '" . $knowId . "' AND `knows_name` = '" . $knowName . "' AND `sub_id` = '" . $subId . "'";
		} else {
			$sql = "SELECT * FROM `knowledge` WHERE `knows_name` = '" . $knowName . "' AND `sub_id` = '" . $subId . "'";
		}
		return $this->fetchAll ( $sql );
	}
}